-- ======================================================================
-- Hell Logic Sniffer Addon
--
-- Copyright 2016 Hell-Prototypes
--
-- http://www.hellprototypes.com/
-- ======================================================================

ModuleClass = "LS_DECODER"
ModuleName  = "CUSTOM"
ModuleAlias = "Pulse Time"
ModuleParam1= "Target CH:"
ModuleTip   = [[
计算指定通道所有高低电平脉宽的时间
]]


function get_edge_list()
	local g_ch_data = {}
	local g_ch_num = LS_PARAM[1]
	--LS_API.DebugMsg("g_ch_num ".. g_ch_num)
	if g_ch_num < 0 then
		LS_API.MessageBox("<MagCard>Get current channel fail...")
		return nil
	end

	--get channel data
	g_ch_data = LS_API.GetChannelData(g_ch_num)
	if type(g_ch_data) ~= "table" then
    	LS_API.MessageBox("<MagCard>Read channel data fail...")
		return nil
    end
	--LS_API.DebugMsg("g_ch_data ".. #g_ch_data)
	local edge_list = {}
	local edge_list_idx = 1
	for idx=1, #g_ch_data, 2 do
		edge_list[edge_list_idx] = g_ch_data[idx]
		edge_list_idx = edge_list_idx + 1
	end

	return edge_list
end

function time_to_readable(time_ns)
	local time_us = time_ns/1000
	local time_ms = time_us/1000
	local time_s  = time_us/1000
	local d = 0
	local f = 0

	if time_ns < 1000 then
		return string.format("%dns", time_ns)
	elseif time_us < 1000 then
		d,f = math.modf(time_us)
		if f == 0 then
			return string.format("%dus", time_us)
		else
			return string.format("%.3fus", time_us)
		end
	elseif time_ms < 1000 then
		d,f = math.modf(time_ms)
		if f == 0 then
			return string.format("%dms", time_ms)
		else
			return string.format("%.3fms", time_ms)
		end
	end
	
	d,f = math.modf(time_s)
	if f == 0 then
		return string.format("%ds", time_ms)
	else
		return string.format("%.3fs", time_ms)
	end
end

function get_pulse_time_and_edge_list()
	local g_sampling_period = 0
	local common_param = LS_API.GetCommonParameter()
	--LS_API.DebugMsg("common_param len = ".. #common_param)
	if #common_param > 0 then
		g_sampling_period = common_param[1]
	end

	local edge_list = get_edge_list()
	if not edge_list then
		return nil
	end

	local pulse_time_and_edge_list = {}
	local list_idx = 1
	local last_edge = edge_list[1]
	for i=2, #edge_list do
		pulse_time_and_edge_list[list_idx] = {PulseTime = 0, Offset = 0}
		pulse_time_and_edge_list[list_idx].PulseTime = (edge_list[i] - last_edge)*g_sampling_period
		pulse_time_and_edge_list[list_idx].Offset = edge_list[i-1]
		last_edge = edge_list[i]
		list_idx = list_idx + 1
	end

	return pulse_time_and_edge_list
end

function main ()
	local pulse_time_and_edge_list = get_pulse_time_and_edge_list()
	if not pulse_time_and_edge_list then
		return
	end

	decode_out = {}
	--LS_API.DebugMsg("pulse_time_and_edge_list size = ".. #pulse_time_and_edge_list)
	for idx=1, #pulse_time_and_edge_list do
		decode_out[1] = pulse_time_and_edge_list[idx].Offset
		decode_out[2] = time_to_readable(pulse_time_and_edge_list[idx].PulseTime)
		decode_out[3] = pulse_time_and_edge_list[idx].Offset
		--LS_API.DebugMsg("decode_out[1] = ".. decode_out[1])
		LS_API.DecodeElementPushBack(decode_out)
	end
end
